package com.bss.service.impl;

import com.bss.dao.ProductMapper;
import com.bss.pojo.Product;
import com.bss.pojo.ProductExample;
import com.bss.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author 袁飞
 * @company 中原工学院
 * @create 2020-03-24-21:17
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    public int addProduct(Product product) {
        return productMapper.insert(product);
    }

    public int updateProduct(Product product) {
        return productMapper.updateByPrimaryKeySelective(product);
    }

    public Product findProductById(Integer productId) {
        return productMapper.selectByPrimaryKey(productId);
    }

    public int deleteProductById(Integer productId) {
        return productMapper.deleteByPrimaryKey(productId);
    }

    public List<Product> findAll() {
        ProductExample productExample = new ProductExample();
        ProductExample.Criteria criteria = productExample.createCriteria();
        return productMapper.selectByExample(productExample);
    }

    /**
     * 查询商品关联类目表
     * @return
     */
    public List<Product> findAllWithCategory() {
        return productMapper.selectAllWithCategory();
    }

    public List<Product> findProductsByCategoryType(Integer categoryType) {
        ProductExample productExample = new ProductExample();
        ProductExample.Criteria criteria = productExample.createCriteria();
        criteria.andCategoryTypeEqualTo(categoryType);
        return productMapper.selectByExample(productExample);
    }


    public BigDecimal calculatePrice(Integer id, Integer num) {
        Product product = productMapper.selectByPrimaryKey(id);
        BigDecimal price = product.getProductPrice();
        BigDecimal count = new BigDecimal(num.toString());
        return price.multiply(count);
    }

    public Product findProductByName(String productName) {
        return productMapper.selectProductByName(productName);
    }

    public List<Product> findProduct(Integer categoryType, String productName, BigDecimal MaxproductPrice, BigDecimal MinproductPrice) {
        ProductExample productExample = new ProductExample();
        ProductExample.Criteria criteria = productExample.createCriteria();
        criteria.andCategoryTypeEqualTo(categoryType);
        criteria.andProductNameLike("%"+productName+"%");
        criteria.andProductPriceLessThanOrEqualTo(MaxproductPrice);
        criteria.andProductPriceGreaterThanOrEqualTo(MinproductPrice);
        return productMapper.selectByExample(productExample);
    }

    public List<Product> findProductStock(String productName, BigDecimal MaxproductPrice, BigDecimal MinproductPrice) {
        ProductExample productExample = new ProductExample();
        ProductExample.Criteria criteria = productExample.createCriteria();
        criteria.andProductNameLike("%"+productName+"%");
        criteria.andProductPriceLessThanOrEqualTo(MaxproductPrice);
        criteria.andProductPriceGreaterThanOrEqualTo(MinproductPrice);
        return productMapper.selectByExample(productExample);
    }

}
